package com.orange.core.service.floor.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.core.service.floor.FloorService;
import com.orange.core.mapper.floor.FloorMapper;
import com.orange.core.mapper.mallArea.MallAreaMapper;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.entity.floor.Floor;
import com.orange.core.pojo.entity.floor.dto.FloorDTO;
import com.orange.core.pojo.entity.floor.vo.FloorVO;
import com.orange.core.pojo.entity.mallArea.MallArea;
import com.orange.core.pojo.req.floor.FloorPageReq;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author lwm
* @description 针对表【floor(楼层表)】的数据库操作Service实现
* @createDate 2025-05-24 18:01:27
*/
@Service
@RequiredArgsConstructor
public class FloorServiceImpl extends ServiceImpl<FloorMapper, Floor>
    implements FloorService {

    private final MallAreaMapper mallAreaMapper;


    @Override
    public Page<FloorVO> page(PageReqDto<FloorPageReq> pageReqDto) {
        String floorName = Optional.ofNullable(pageReqDto.getCondition()).map(FloorPageReq::getFloorName).orElse(null);
        Page<Floor> page = this.lambdaQuery().like(StrUtil.isNotBlank(floorName), Floor::getFloorName, floorName)
                .orderByAsc(Floor::getAreaId,Floor::getSortOrder)
                .page(pageReqDto.toPage());

        Page<FloorVO> floorVOPage = new Page<>(pageReqDto.getPage(), pageReqDto.getSize(), page.getTotal());

        //获取所有的区域id
        Set<Integer> areaId = page.getRecords().stream()
                .map(Floor::getAreaId).collect(Collectors.toSet());

        //获取所有的区域信息组合成map集合
        Map<Integer, MallArea> mallAreaMap = mallAreaMapper.selectBatchIds(areaId).stream()
                .collect(Collectors.toMap(MallArea::getId, mallArea -> mallArea));

      /*  List<FloorVO> floorVOList = ConvertUtils.convertListToVo(page.getRecords(), FloorVO.class)
                        .stream().map(floorVO -> {
                           floorVO.setAreaName(Optional.ofNullable(mallAreaMap.get(floorVO.getAreaId()))
                                   .map(MallArea::getAreaName).orElse(null));
                           return floorVO;
                }).collect(Collectors.toList());*/

        List<FloorVO> floorVOList = page.getRecords().stream()
                .sorted(Comparator.comparing(Floor::getAreaId))
                .map(floor -> {
                    FloorVO floorVO = new FloorVO();
                    floorVO.setFloorId(floor.getId());
                    floorVO.setFloorName(floor.getFloorName());
                    floorVO.setAreaId(floor.getAreaId());
                    //floorVO.setAreaName(mallAreaMap.get(floor.getAreaId()).getAreaName());
                    floorVO.setAreaName(Optional.ofNullable(mallAreaMap.get(floor.getAreaId()))
                            .map(MallArea::getAreaName).orElse(null));

                    floorVO.setSortOrder(floor.getSortOrder());
                    return floorVO;
                }).collect(Collectors.toList());

        floorVOPage.setRecords(floorVOList);
        return floorVOPage;
    }

    @Override
    public FloorVO info(Integer id) {
        Floor floor = this.getById(id);
        Assert.notNull(floor,"楼层不存在");
        FloorVO floorVO = new FloorVO();
        BeanUtil.copyProperties(floor, floorVO);
        MallArea mallArea = mallAreaMapper.selectById(floor.getAreaId());
        floorVO.setAreaName(mallArea.getAreaName());
        return floorVO;
    }

    @Override
    @Transactional
    public Boolean save(FloorDTO floorDTO) {
        Floor floor = new Floor();
        BeanUtil.copyProperties(floorDTO, floor);
        boolean result = this.save(floor);
        return result;
    }

    @Override
    @Transactional
    public Boolean update(FloorDTO floorDTO) {
        Floor floor = new Floor();
        BeanUtil.copyProperties(floorDTO, floor);
        boolean result = this.updateById(floor);
        return result;
    }

    @Override
    @Transactional
    public Boolean deleteFloorByIds(Integer[] ids) {
        boolean result = this.removeBatchByIds(Arrays.asList(ids));
        return result;
    }

    @Override
    public List<FloorVO> floorList() {

        List<Floor> floorList = this.lambdaQuery().orderByAsc(Floor::getAreaId,Floor::getSortOrder).list();

        List<Integer> areaIds = floorList.stream().map(Floor::getAreaId).distinct().collect(Collectors.toList());

        Map<Integer, MallArea> mallAreaMap = mallAreaMapper.selectBatchIds(areaIds).stream()
                .collect(Collectors.toMap(MallArea::getId, mallArea -> mallArea));

        List<FloorVO> floorVOS = floorList.stream().map(floor -> {
            FloorVO floorVO = new FloorVO();
            floorVO.setFloorId(floor.getId());
            floorVO.setFloorName(floor.getFloorName());
            floorVO.setAreaId(floor.getAreaId());
            floorVO.setAreaName(Optional.ofNullable(mallAreaMap.get(floor.getAreaId()))
                    .map(MallArea::getAreaName).orElse(null));
            return floorVO;
        }).collect(Collectors.toList());
        return floorVOS;
    }


}




